package model

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/entity"
	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
)

// 消息通知
func (a *GroupNotification) Message(ctx context.Context, params schema.GroupNotificationQueryParam, opts ...schema.GroupNotificationQueryOptions) (*schema.GroupNotificationQueryResult, error) {
	db := entity.GetGroupNotificationDB(ctx, a.DB)
	// TODO: 查询条件
	db = db.Where("`group_id` = ?", params.GroupId).Where("`type` = ?", params.Type)
	if v := params.Creator; v != "" {
		db = db.Where("`creator` = ?", v)
	}
	opt := a.getQueryOption(opts...)
	opt.OrderFields = append(opt.OrderFields, schema.NewOrderField("id", schema.OrderByDESC))
	db = db.Order(ParseOrder(opt.OrderFields))

	var list entity.GroupNotifications
	pr, err := WrapPageQuery(ctx, db, params.PaginationParam, &list)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	qr := &schema.GroupNotificationQueryResult{
		PageResult: pr,
		Data:       list.ToSchemaGroupNotifications(),
	}

	return qr, nil
}
